package com.apestech.framework.file.modul;

import java.util.ArrayList;

import com.apestech.framework.json.JSONObject;

public class PageModul {
	
	private int count=0;
	private ArrayList<Integer> list=new ArrayList<Integer>();
	private int fetchSize=100;
	
	public PageModul(){
	}
	
	public PageModul(int fetchSize){
		this.fetchSize=fetchSize;
		if(this.fetchSize<30){
			this.fetchSize=30;
		}
	}
	
	public void setFetchSize(int fetchSize){
		this.fetchSize=fetchSize;
	}
	
	private void getList(int index){
		int len=this.list.size();  
		int z=index+1-len; 
		if(z>0){
			for(int i=0; i<z; i++){
				addPage(0);
			}
		}
	}
	
	public JSONObject toJSON(){
		JSONObject map=new JSONObject();
		map.put("count", this.count);
		map.put("list", this.list);
		map.put("fetchSize", this.fetchSize);
		return map;
	}
	
	public void fromJSON(JSONObject map) throws Exception{
		if(map==null) return;
		this.count=(int) map.get("count");
		this.list=(ArrayList<Integer>) map.get("list");
		this.fetchSize=(int) map.get("fetchSize");
	}
	
	public int addRow(){
		this.count++;
		int inx=0;
		inx=(this.count-1) / this.fetchSize;
		if(inx>this.list.size()-1){
			this.list.add(0);
		}
		getList(inx);
		this.list.set(inx, ((this.count-1) % this.fetchSize)+1);
		return inx;
	}
	
	public int insRow(){
		int len=this.list.size();
		if(len>0){
			for(int i=0; i<len; i++){
				int row=this.list.get(i);
				if(row<this.fetchSize){
					this.count++;
					this.list.set(i, row+1);
					return i;
				}
			}
		}
		return addRow();
	}
	
	public int removeRow(int index){
		this.count--;
		int inx=0;
		inx=index / this.fetchSize;
		this.list.set(inx, this.list.get(inx)-1);
		return inx;
	}
	
	public int getCount() {
		return count;
	}
	public void setCount(int count) {
		this.count = count;
	}
	
	public void addPage(int row){
		this.list.add(row);
		this.count=this.count+row;
	}
	
	public int getPage(int index){
		if(this.list.size()<index+1) return 0;
		return this.list.get(index);
	}
	
	public void setPage(int index, int row){
		getList(index);
		int old=this.list.get(index);
		this.list.set(index, row);
		this.count=this.count-old+row;
	}
	
	public int getFileCount(){
		return this.list.size();
	}
	
	public IndexModul getFileIndex(int index){
		IndexModul p=new IndexModul();
		p.setFileIndex(index / this.fetchSize);
		p.setIndex((index % this.fetchSize));
		return p;
	}
	
	public String toString(){
		StringBuilder sb=new StringBuilder();
		sb.append("\rcount: "+getCount());
		sb.append("\rfile count: "+getFileCount());
		int findex=-1;
		for(int i=0; i<this.getFileCount(); i++){
			sb.append("\r第"+(i+1)+"页"+getPage(i));
		}
		return sb.toString();
	}
	
	public String getFileIndexString(int index){
		IndexModul p=getFileIndex(index);
		StringBuilder sb=new StringBuilder();
		sb.append("\rcount: "+getCount());
		sb.append("\rfile count: "+getFileCount());
		sb.append("\rindex: "+index);
		sb.append("\rfileIndex: "+p.getFileIndex());
		sb.append("\rindex: "+p.getIndex());
		return sb.toString();
	}

}
